package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.travel.dao.IAllianceSetlDetailDao;
import com.eling.elcms.travel.model.AllianceConfirm;
import com.eling.elcms.travel.model.AllianceSetlDetail;
import com.eling.elcms.travel.model.RuleType;
import com.eling.elcms.travel.model.SignUpCharge;
import com.eling.elcms.travel.service.IAllianceSetlDetailManager;
import com.eling.elcms.travel.service.IAllianceSettlementManager;
import com.eling.elcms.travel.service.ISignUpChargeManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class AllianceSetlDetailManagerImpl extends GenericManagerImpl<AllianceSetlDetail, Long>
		implements IAllianceSetlDetailManager {

	IAllianceSetlDetailDao allSetlDetlDao;

	@Autowired
	private ISignUpChargeManager sucManager;

	@Autowired
	private IAllianceSettlementManager asManager;

	@Autowired
	public void setAllianceSetlDetailDao(IAllianceSetlDetailDao allSetlDetlDao) {
		this.dao = allSetlDetlDao;
		this.allSetlDetlDao = allSetlDetlDao;
	}

	/**
	 * 入住联盟确认时推送结算单
	 * 
	 * @author zhoulin
	 */
	@Override
	public List<AllianceSetlDetail> SendAllSetlDetail(AllianceConfirm ac) {
		Long pkPlanItemGroup = ac.getGroupReserve().getPlanItemGroup().getPkPlanItemGroup();
		SignUpCharge sucCond = new SignUpCharge();
		PropertyUtils.setProperty(sucCond, "signUpRegister.planItemGroup.pkPlanItemGroup", pkPlanItemGroup);
		// 查询所有收费单
		List<SignUpCharge> sucList = sucManager.query(sucCond);
		List<AllianceSetlDetail> asdList2 = new ArrayList<AllianceSetlDetail>();
		if (!sucList.isEmpty()) {
			List<Long> pkSUCList = sucList.stream().map(a -> a.getPkSignUpCharge()).collect(Collectors.toList());
			AllianceSetlDetail asdCond = new AllianceSetlDetail();
			PropertyUtils.setProperty(asdCond, "signUpCharge.pkSignUpChargeIn", pkSUCList);
			// 查询所有结算单
			List<AllianceSetlDetail> asdList1 = allSetlDetlDao.query(asdCond);

			List<Long> pksucList = new ArrayList<>();
			if (!asdList1.isEmpty()) {
				asdList1.stream().map(b -> b.getSignUpCharge().getPkSignUpCharge()).collect(Collectors.toList());
			}
			Set<Long> pksucSet = new HashSet<>(pksucList);

			for (SignUpCharge suc : sucList) {
				boolean flag = true;
				// 判断收费单在结算单中是否已存在，不存在则推送收费单
				if (!pksucSet.isEmpty() && pksucSet.contains(suc.getPkSignUpCharge())) {
					flag = false;
				}
				if(suc.getSignUpRegister().getExitGroup() != null && suc.getSignUpRegister().getExitGroup()){
					flag = false;
				}
				if (flag) {
					AllianceSetlDetail asd = new AllianceSetlDetail();
					//获取权证抵扣
					asd.setDeductionCharges(
							asManager.getAllianceRuleMoney(suc.getSignUpRegister(), RuleType.Expense,ac.getGroupReserve()).getMoney());
					asd.setSignUpCharge(suc);
					asd.setFinish(false);
					asd.setVersion(0);
					asdList2.add(asd);
				}
			}
			if (!asdList2.isEmpty()) {
				asdList2 = allSetlDetlDao.save(asdList2);
			}
		}
		return asdList2;
	}

}
